package com.itheima.dao;

import com.itheima.pojo.Order;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;
@Mapper
public interface OrderDao {
    //新增
    @Insert("<selectKey resultType=\"java.lang.Integer\" order=\"AFTER\" keyProperty=\"id\">\n" +
            "            SELECT LAST_INSERT_ID()\n" +
            "        </selectKey>\n" +
            "        insert into t_order(member_id,orderDate,orderType,orderStatus,setmeal_id)\n" +
            "        values (#{memberId},#{orderDate},#{orderType},#{orderStatus},#{setmealId})")
    void add(Order order);
    //动态条件查询
    @Select("<script> select * from t_order\n" +
            "        <where>\n" +
            "            <if test=\"id != null\">\n" +
            "                and id = #{id}\n" +
            "            </if>\n" +
            "            <if test=\"memberId != null\">\n" +
            "                and member_id = #{memberId}\n" +
            "            </if>\n" +
            "            <if test=\"orderDate != null\">\n" +
            "                and orderDate = #{orderDate}\n" +
            "            </if>\n" +
            "            <if test=\"orderType != null\">\n" +
            "                and orderType = #{orderType}\n" +
            "            </if>\n" +
            "            <if test=\"orderStatus != null\">\n" +
            "                and orderStatus = #{orderStatus}\n" +
            "            </if>\n" +
            "            <if test=\"setmealId != null\">\n" +
            "                and setmeal_id = #{setmealId}\n" +
            "            </if>\n" +
            "        </where> </script>")//注意数据是否自动封装
    List<Order> findByCondition(Order order);
    //根据预约id查询预约信息，包括体检人信息、套餐信息，（字段别名跟页面名称一致）
    @Select("select m.name member ,s.name setmeal,o.orderDate orderDate,o.orderType orderType\n" +
            "        from t_order o, t_member m, t_setmeal s where o.member_id=m.id and o.setmeal_id=s.id and o.id=#{id}")
    Map findById4Detail(Integer id);
    //根据日期统计预约数
    @Select("select count(id) from t_order where orderDate = #{value}")
    Integer findOrderCountByDate(String date);
    //根据日期统计预约数，统计指定日期之后的预约数
    @Select("select count(id) from t_order where orderDate &gt;= #{value}")
    Integer findOrderCountAfterDate(String date);
    //根据日期统计到诊数
    @Select("select count(id) from t_order where orderDate = #{value} and orderStatus = '已到诊'")
    Integer findVisitsCountByDate(String date);
    //根据日期统计到诊数，统计指定日期之后的到诊数
    @Select("select count(id) from t_order where orderDate &gt;= #{value} and orderStatus = '已到诊'")
    Integer findVisitsCountAfterDate(String date);
    //热门套餐，查询前5条
    @Select("select s.name, count(o.id) setmeal_count ,count(o.id)/(select count(id) from t_order) proportion\n" +
            "          from t_order o inner join t_setmeal s on s.id = o.setmeal_id group by o.setmeal_id order by setmeal_count desc limit 0,4")
    List<Map> findHotSetmeal();
}
